﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ThermalMarker.Entities;

namespace ThermalMarker.Utils
{
    class MathFunctions
    {
        const double RADIO = 6371;

        static public double DistanceBetweenWPS(WayPoint x, WayPoint y)
        {
            double dLat = toRadian(y.Latitude - x.Latitude);
            double dLon = toRadian(y.Longitude - x.Longitude);
            double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
            Math.Cos(toRadian(x.Latitude)) * Math.Cos(toRadian(y.Latitude)) *
            Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
            double c = 2 * Math.Asin(Math.Min(1, Math.Sqrt(a)));
            double d = RADIO * c;
            return d;
        }

        static public double ParseLatLng(string value)
        {
            if (value.Length == 8)
            {
                //Its a Latitude Value
                double deg = double.Parse(value.Substring(0,2));
                double min = double.Parse(value.Substring(2,2));;
                double sec = double.Parse(value.Substring(4,3)); ;
                char dir = value[7];
                double result = deg + (min / 60) + (sec / 3600);

                if (dir == 'S')
                    return -result;
                else
                    return result;

            }
            else
            {
                //Its a Longitude Value
                double deg = double.Parse(value.Substring(0, 3));
                double min = double.Parse(value.Substring(3, 2)); ;
                double sec = double.Parse(value.Substring(5, 3)); ;
                char dir = value[8];
                double result = deg + (min / 60) + (sec / 3600);

                if (dir == 'W')
                    return -result;
                else
                    return result;
            }
        }
        
        static double toRadian(double val)
        {
            return (Math.PI / 180) * val;
        }

    }
}
